<?php
use app\common\config\autoreply_config;
use app\common\helper\index_msg_helper;
use app\common\helper\appmsg_helper;
use app\common\helper\autoreply_helper;
?>
{include file="../app/common/admin/lib/header.html"  title="自动回复"}
{include file="../app/autoreply/view/lib/top_menu.html"}
<div class="rlbox">
  <!--start:关键词回复列表-->
  <div class="page-header clearfix">
    <div class="page-header-intro left">在这里设置：通过匹配用户输入的内容，来自动回复<br>例如：当用户输入“会员”，会查找是否设置了会员关键词，如果有，则回复对应的信息</div>
    <a href='{:url("/autoreply/add/3")}' class="btn btn-success btn-sm right">添加规则</a>
  </div>
  <div class="search-level1 clearfix">
    <form action='{:url("/autoreply/admin/3")}' method="get">
      <input type="hidden" name="is_advenced" value="0">
      <input {if $is_advenced == 1}style="display:none;"{/if} type="text" class="left" name="search[title]" value="{$search_filter.title}">
      <button {if $is_advenced == 1}style="display:none;"{/if} type="submit" class="btn btn-success btn-sm left">搜索</button>
      <a href="javascript:void(0);" class="close js_advanced {if $is_advenced == 1}js_isOpen open{/if}">高级搜索<em></em></a>
    </form>
  </div>
  <div class="search-form js_advancedForm" {if $is_advenced == 0}style="display:none;"{/if}>
    <form action='{:url("/autoreply/admin/3")}' method="get">
      <input type="hidden" name="is_advenced" value="1">
      <p>
        <label for="" class="search-f-label">规则名称：</label>
        <input type="text" class="search-f-ipt1" name="search[title]" value="{$search_filter.title}">
      </p>
      <p>
      <label for="" class="search-f-label">添加时间：</label>
        <input type="text" class="search-f-ipt1 search-f-ipt2" name="search[start_date]" value="{$search_filter.start_date}" id="startDate">
        <span class="search-f-line"></span>
        <input type="text" class="search-f-ipt1 search-f-ipt2" name="search[stop_date]" value="{$search_filter.stop_date}" id="stopDate">
      </p>
      <p><label for="" class="search-f-label"></label><button type="submit" class="btn btn-success btn-sm js_searchBtn">搜索</button></p>
    </form>
  </div>
  <table class="list-table table-sm table-sm01 table table-bordered" cellpadding="0" cellspacing="0" style="width:100%;">
    <thead>
      <tr>
        <th style="width:16%;">规则名</th>
        <th style="width:250px;">关键词</th>
        <th style="width:120px;">关键词对应的回复</th>
        <th style="width:125px;">添加时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
    {if !empty($list['data'])}
      {foreach list['data'] as $k=>$v}
          <tr data-id="{$v.id}">
            <td>{if !$v.title}--{else}{$v.title}{/if}</td>
            <td {$content_count = autoreply_helper::get_autoreply_content_count($v.id)} {if $content_count != 1} valign="top" {/if}>
              {$key_list = autoreply_helper::get_autoreply_key_list($v.id, 2)}
              {if $key_list}
                {foreach $key_list as $k1 => $v1}
                  <div class="list-per-keywords js_perKey" data-id="{$v1.id}" data-match="{$v1.match_type}" {if !isset($key_list[$k1+1])}style="border-bottom:0 none;"{/if}>
                    <span style="display:inline-block;width:150px;overflow:hidden;">{$v1.title}</span>
                    <span class="js_perMatch" style="color:#39b3d7; font-size:12px; cursor:pointer; display:none;">{if $v1.match_type == 1}未全匹配{else}全部匹配{/if}</span>
                  </div>
                {/foreach}
              {/if}
            </td>
            <td>{:autoreply_helper::get_autoreply_content_str($v.id)}</td>
            <td>{:time_format($v.add_time)}</td>
            <td>
              <a href='{:url("/autoreply/add/3", ["id"=>$v.id])}' class="btn btn-info btn-xs">编辑</a>
              <a href="javascript:void(0);" class="btn btn-danger btn-xs js_deleteKey" data-id="{$v.id}">删除</a>
            </td>
          </tr>
      {/foreach}
    {else /}
        <tr><td colspan="5">暂无数据</td></tr>
    {/if}
    </tbody>
  </table>

  <!--end:关键词回复列表-->
</div>

<div id="pages"></div>
<script>
$(function(){
  // 高级搜索
  $('.js_advanced').click(function(e){
    e.preventDefault();
    var thisObj = $(this);
    var outBox  = thisObj.closest('form');
    if (thisObj.hasClass('js_isOpen')) {
      thisObj.removeClass('js_isOpen open');
      outBox.find('input').show();
      outBox.find('button').show();
      $('.js_advancedForm').hide();
    } else {
      thisObj.addClass('js_isOpen open');
      outBox.find('input').hide();
      outBox.find('button').hide();
      $('.js_advancedForm').show();
    }
  });

  // 匹配的显示与隐藏
  $(".js_perKey").mouseover(function(){
    $(this).find(".js_perMatch").show();
  }).mouseout(function(){
    $(this).find(".js_perMatch").hide();
  })
  
  // 匹配情况调整
  $(".js_perMatch").click(function(){
    var thisObj = $(this);
    var thisClosest = thisObj.closest('.js_perKey');

    if (thisClosest.attr('is-post') == 1) {
      showMsg('数据提交中', 0);
      return;
    }
    
    var id         = thisClosest.closest('tr').attr('data-id');
    var matchType  = thisClosest.attr('data-match');
    var keyId      = thisClosest.attr('data-id');
    
    thisClosest.attr('is-post', 1);
    var postData = { 'id':id, 'match_type':matchType, 'key_id':keyId };
    $.post(siteUrl+'/autoreply/admin/change_match_type', postData, function(json){
      thisClosest.attr('is-post', 0);
      if (json.info != 'ok') {
        showMsg(json.info, 0);
        return;
      }
      
      thisClosest.attr('data-match', json.match_type);
      if (json.match_type == 2) {
        thisObj.html('全部匹配');
      } else {
        thisObj.html('未全匹配');
      }
    }, 'json');
  });
  
  // 高级搜索时间验证
  $('.js_searchBtn').click(function(e){
    e.preventDefault();
    var thisObj   = $(this);
    var startDate = $.trim($('#startDate').val());
    var stopDate  = $.trim($('#stopDate').val());
    if (startDate > stopDate) {
      showMsg('结束时间应大于等于开始时间');
      return;
    }
    thisObj.closest('form').submit();
  });
  
  var isPost = false;
  $('.js_deleteKey').click(function(e){
    e.preventDefault(); 
    var thisObj = $(this);
    var id = thisObj.attr('data-id');
    showConfirm('您确定要删除该关键字吗？', 1, function(){
      if (isPost) {
        showMsg('正在提交中，请稍候');
        return;
      }
      isPost = true;
      $.post(
        siteUrl+'/autoreply/delete/3',
        {
          'id':id,
          'type':'key'
        },
        function(json) {
          isPost = false;
          if (json.info != 'ok') {
            showMsg(json.info);
            return;
          }
          thisObj.closest('tr').remove();
        },'json'
      );
    });
  });
});
</script>
{include file="../app/common/admin/lib/footer.html"}